package com.atguigu.hellomp;

import com.atguigu.hellomp.entity.User;
import com.atguigu.hellomp.mapper.UserMapper;
import com.atguigu.hellomp.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
import java.util.function.Consumer;

@SpringBootTest
public class DemoVst {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserService userService;

    @Test
    public void test1() {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("name", "Tom");
        List<User> users = userMapper.selectList(userQueryWrapper);
        System.out.println(users);
    }
    @Test
    public void test2() {
        QueryWrapper<User> userQueryWrapper2 = new QueryWrapper<>();
        userQueryWrapper2.like("email", "baomidou.com");
        List<User> users = userMapper.selectList(userQueryWrapper2);
        System.out.println(users);
    }
    @Test
    public void test3() {
        QueryWrapper<User> userQueryWrapper3 = new QueryWrapper<>();
        //查询所有用户信息并按照age字段降序排序
        userQueryWrapper3.orderByDesc("age");
        List<User> users = userMapper.selectList(userQueryWrapper3);
        System.out.println(users);
    }
    @Test
    public void test4() {
        QueryWrapper<User> userQueryWrapper4 = new QueryWrapper<>();
        //查询age介于[20,30]的所有用户
        userQueryWrapper4.ge("age", 20);
        userQueryWrapper4.le("age", 30);
        List<User> users = userMapper.selectList(userQueryWrapper4);
        System.out.println(users);
    }
    @Test
    public void test5() {
        QueryWrapper<User> userQueryWrapper4 = new QueryWrapper<>();
        //查询age小于20或大于30的用户
        userQueryWrapper4.lt("age", 20).or().gt("age", 30);
        List<User> users = userMapper.selectList(userQueryWrapper4);
        System.out.println(users);
    }
    @Test
    public void test6() {
        QueryWrapper<User> userQueryWrapper5 = new QueryWrapper<>();
        //邮箱域名为baomidou.com且年龄小于30或大于40的用户
//        Consumer<QueryWrapper<User>> queryWrapperConsumer = new Consumer<QueryWrapper<User>>() {
//            @Override
//            public void accept(QueryWrapper<User> userQueryWrapper) {
//                userQueryWrapper.lt("age", 30).or().gt("age", 40);
//            }
//        };
        userQueryWrapper5.like("email", "baomidou.com").and(wrapper -> wrapper.lt("age", 30).or().gt("age", 40));
        List<User> users = userMapper.selectList(userQueryWrapper5);
        System.out.println(users);
    }
    @Test
    public void test7() {
        UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
        //将name=Tom的用户的email改为Tom@baobidou.com
        userUpdateWrapper.eq("name", "Tom").set("email", "Tom@baomidou.com");
        boolean update = userService.update(userUpdateWrapper);
        System.out.println("更新状态 = "+update);
    }
    @Test
    public void test8() {
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getName, "Tom");
        List<User> users = userService.list(userLambdaQueryWrapper);
        users.forEach(System.out::println);
    }
    @Test
    public void test9() {
        //将name=Tom的用户的邮箱改为Tom@tom.com
        LambdaUpdateWrapper<User> userLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        userLambdaUpdateWrapper.eq(User::getName, "Tom");
        userLambdaUpdateWrapper.set(User::getEmail, "Tom@tom.com");
        boolean update = userService.update(userLambdaUpdateWrapper);
        System.out.println("更新状态 = " + update);
    }
}
